<?php
namespace app\single\controller;
/**
 * #米客微信 {用户存贮获取微信好友、群组、小程序相关}
 */
class Mikeweixin extends Common{
    /**
     * 获取数据
     * @param  string  $key  [description]
     * @param  integer $type -1：所有 0：好友 1：群组 2：小程序
     * @param  integer $isDel -1：所有 0：未删除 1：已删除
     * @return [type]        [description]
     */
    function index($key='',$type=-1,$isDel=0){
        $uid=$this->user['id'];
        $map=[
            'uid'=>$uid,
        ];
        if(!empty($key)){
            $map['key']=$key;
        }
        if($type>=0){
            $map['type']=$type;
        }
        if($isDel>=0){
            $map['isDelete']=$isDel;
        }
        $list=model($this->table)->readAll($map,true)->toArray();
        return json('',0,$list['data']);
    }
    /**
     * 添加好友数据
     * @param string $key  存贮键值(必选)，通常使用微信号作为key
     * @param integer $type  0：用户，1：群组 2:小程序
     * @param json   $data=[ 需要添加的数据(json)
     *        nickName       varchar(50)   用户名(必选)
     *        userName     varchar(50)   备注
     *        wxId     varchar(50)   微信号
     *        sex        tinyint(1)    性别：0女、1男、2保密
     *        addr       varchar(200)  区域
     * ]  
     */
    function add($key='',$type=0,$data=''){
        if(is_string($data)){
            $data=json_decode($data,true);
        }
        if(empty($key)||empty($data)){
            return json(lang('Data does not exist'),1);
        }
        $time=time();
        $updateData=[];
        $uid=$this->user['id'];

        if(is_integer(key($data))){ //批量添加
            $hasNumber=isset($data[0]['wxId'])&&!empty($data[0]['wxId']);
            if(!$hasNumber){ 
                // 不存在唯一的号码识别（例：微信号）每次更新均做完全替换处理(软删除的不进行替换)
                model($this->table)->del(['key'=>$key,'type'=>$type,'isDelete'=>0]);
            }
            foreach($data as $k => $v){
                $has=[];
                if($hasNumber){
                    $map=[
                        'uid'=>$uid,
                        'key'=>$key,
                        'wxId'=>$v['wxId'],
                    ];
                    // 验证该好友是否添加
                    $has=model($this->table)->read($map);
                }
                if(empty($has)){
                    $data[$k]['uid']=$uid;
                    $data[$k]['key']=$key;
                    $data[$k]['type']=$type;
                    $data[$k]['s_time']=$time;
                }else{
                    $v['id']=$has['id'];
                    $v['isDelete']=0;
                    $updateData[]=$v;
                    unset($data[$k]);
                }
            }
        }else{
            $map=[
                'uid'=>$uid,
                'key'=>$key,
                'type'=>$type,
            ];
            $hasNumber=isset($data['wxId'])&&!empty($data['wxId']);
            if($hasNumber){ 
                $map['wxId']=$data['wxId'];
            }else{
                //群并不存在唯一的识别码 不应该做此更新直接做批量替换
                $map['nickName']=$data['nickName'];
            }
            $data['uid']=$uid;
            $data['key']=$key;
            $data['type']=$type;
            $data['s_time']=$time;
            // 验证该好友是否添加(做更新处理)
            $has=model($this->table)->read($map);
            if(count($has)){
                $data['id']=$has['id'];
                $data['isDelete']=0;
                $updateData=$data;
                $data=[];
            };
        }

        if(!empty($updateData)){
            model($this->table)->edit($updateData);
        }
        if(!empty($data)){
            model($this->table)->add($data);
        }
        return json(lang('Successful operation'));
    }
    /**
     * 删除微信好友
     * @param  string  $key  
     * @param  string  $numbers  微信号的JSON字符串 {0:'nums1',1:'nums2'}
     * @return json
     */
    function destory($key='',$numbers='',$isDel=0){
        if(is_string($numbers)){
            $numbers=json_decode($numbers,true);
        }
        if(empty($key)||empty($numbers)){
            return json(lang('Data does not exist'),1);
        }
        $where=[
            'uid'=>$this->user['id'],
            'key'=>$key,
            'nickName|wxId|userName'=>['nickName|wxId|userName','in',$numbers]
        ];
        if($isDel){
            model($this->table)->del($where);
        }else{
            model($this->table)->edit(['isDelete'=>1],$where);
        }
        return json(lang('Successful operation'));
    }
    /**
     * 恢复微信好友
     * @param  string  $key  
     * @param  string  $numbers  微信号的JSON字符串 {0:'nums1',1:'nums2'}
     * @return json
     */
    function restore($key='',$numbers=''){
        if(is_string($numbers)){
            $numbers=json_decode($numbers,true);
        }
        if(empty($key)||empty($numbers)){
            return json(lang('Data does not exist'),1);
        }
        $where=[
            'uid'=>$this->user['id'],
            'key'=>$key,
            'nickName|wxId|userName'=>['nickName|wxId|userName','in',$numbers]
        ];
        model($this->table)->edit(['isDelete'=>0],$where);
        return json(lang('Successful operation'));
    }
}